﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Transcend.FileUploader.Utility
{
    public class ConvertHelper
    {
        /// <summary>
        /// 将字符串强制转换成decimal
        /// </summary>
        /// <param name="sval">要转换的字符串</param>
        /// <param name="def">转换失败默认值</param>
        /// <returns></returns>
        public static decimal ToDecimal(string sval, decimal def)
        {
            decimal ret = def;
			if (string.IsNullOrEmpty(sval)) return ret;
            try
            {
                ret = Convert.ToDecimal(sval);
            }
            catch (Exception ex)
            {
            }
            return ret;
        }

        public static int ToInt32(string sval, int def)
        {
            int ret = def;
			if (string.IsNullOrEmpty(sval)) return ret;
            try
            {
                ret = Convert.ToInt32(sval);
            }
            catch (Exception ex)
            {
            }
            return ret;
        }

        public static double ToDouble(string sval, double def)
        {
            double ret = def;
			if (string.IsNullOrEmpty(sval)) return ret;
            try
            {
                ret = Convert.ToDouble(sval);
            }
            catch (Exception ex)
            {
            }
            return ret;
        }

        public static bool ToBool(object oval, bool def)
        {
            bool ret = def;
			if (oval == null) return ret;
            try
            {
                ret = Convert.ToBoolean(oval);
            }
            catch (Exception ex)
            {
            }
            return ret;
        }

        /// <summary>
        /// 将对象转换成string，为空则返回string.Empty
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string NullSafeStr(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            return value.ToString();
        }

		public static string ToStr(object val, string def)
		{
			string ret = def;
			if (val != null)
			{
				if (val != DBNull.Value)
				{
					ret = val.ToString();
				}
			}
			return ret;
		}

		public static int ToInt32(object oval, int def)
		{
			int ret = def;
			if (oval == null) return ret;
			try
			{
				if (oval != DBNull.Value)
				{
					ret = Convert.ToInt32(oval);
				}
			}
			catch (Exception ex)
			{
			}
			return ret;
		}

		public static long ToLong(object oval, long def)
		{
			long ret = def;
			if (oval == null) return ret;
			try
			{
				if (oval != DBNull.Value)
				{
					ret = Convert.ToInt64(oval);
				}
			}
			catch (Exception ex)
			{
			}
			return ret;
		}

		public static DateTime ToDateTime(object oval, DateTime def)
		{
			DateTime ret = def;
			if (oval == null) return ret;
			try
			{
				if (oval != DBNull.Value)
				{
					ret = Convert.ToDateTime(oval);
				}
			}
			catch (Exception ex)
			{
			}
			return ret;
		}

		public static decimal ToDecimal(object oval, decimal def)
		{
			decimal ret = def;
			if (oval == null) return ret;
			try
			{
				if (oval != DBNull.Value)
				{
					ret = Convert.ToDecimal(oval);
				}
			}
			catch (Exception ex)
			{
			}
			return ret;
		}
    }
}
